<?php
/**
 * 商家订单
 *
 *
 *
 *
 * @copyright  Copyright (c) 2007-2015 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */



defined('ByShopWWI') or exit('Access Invalid!');

class seller_orderControl extends mobileSellerControl {

    public function __construct(){
        parent::__construct();
    }

    public function order_listOp() {
        $model_order = Model('order');

        $order_list = $model_order->getStoreOrderList(
            $this->store_info['store_id'],
            $_POST['order_sn'],
            $_POST['buyer_name'],
            $_POST['state_type'],
            $_POST['query_start_date'],
            $_POST['query_end_date'],
            $_POST['skip_off'],
            '*',
            array('order_goods')
        );

        $page_count = $model_order->gettotalpage();

        output_data(array('order_list' => $order_list), mobile_page($page_count));
    }

    /**
     * 取消订单
     */
    public function order_cancelOp() {
        $order_id = intval($_POST['order_id']);
        $reason = $_POST['reason'];
        $model_order = Model('order');
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['store_id'] = $this->store_info['store_id'];
        $order_info = $model_order->getOrderInfo($condition);

        $if_allow = $model_order->getOrderOperateState('store_cancel',$order_info);
        if (!$if_allow) {
            output_error('无权操作');
        }

        if (TIMESTAMP - 86400 < $order_info['api_pay_time']) {
            $_hour = ceil(($order_info['api_pay_time']+86400-TIMESTAMP)/3600);
            output_error('该订单曾尝试使用第三方支付平台支付，须在'.$_hour.'小时以后才可取消');
        }

        if ($order_info['order_type'] == 2) {
            //预定订单
            $result = Logic('order_book')->changeOrderStateCancel($order_info,'seller',$this->seller_info['seller_name'], $reason);
        } else {
            $cancel_condition = array();
            if ($order_info['payment_code'] != 'offline') {
                $cancel_condition['order_state'] = ORDER_STATE_NEW;
            }
            $result = Logic('order')->changeOrderStateCancel($order_info,'seller',$this->seller_info['seller_name'], $reason, true, $cancel_condition);
        }

        if (!$result['state']) {
            output_error($result['msg']);
        }
        output_data('1');
    }
    
    /**
     * 修改运费
     */
    public function order_ship_priceOp() {
        $order_id = intval($_POST['order_id']);
        $shipping_fee = ncPriceFormat($_POST['shipping_fee']);
        $model_order = Model('order');
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['store_id'] = $this->store_info['store_id'];
        $order_info = $model_order->getOrderInfo($condition);

        $if_allow = $model_order->getOrderOperateState('modify_price',$order_info);
        if (!$if_allow) {
            output_error('无权操作');
        }
        $result = Logic('order')->changeOrderShipPrice($order_info, 'seller', $this->seller_info['seller_name'], $shipping_fee);

        if (!$result['state']) {
            output_error($result['msg']);
        }
        output_data('1');
    }
    
    /**
     * 发货
     */
    public function order_deliver_sendOp() {
        $order_id = intval($_POST['order_id']);
        $model_order = Model('order');
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['store_id'] = $this->store_info['store_id'];
        $order_info = $model_order->getOrderInfo($condition,array('order_common','order_goods'));
        $if_allow_send = intval($order_info['lock_state']) || !in_array($order_info['order_state'],array(ORDER_STATE_PAY,ORDER_STATE_SEND));
        if ($if_allow_send) {
            output_error('无权操作');
        }

        $result = Logic('order')->changeOrderSend($order_info, 'seller', $this->seller_info['seller_name'], $_POST);
        if (!$result['state']) {
            output_error($result['msg']);
        }
        output_data('1');
    }
	/*线下收银*/
	function line_cashOp(){
		$model_line =Model('line_cash'); 
		$member_id = $this->member_info['member_id'];
		$model_member = Model('member');
		$contion='member_id ='.$member_id;
		$member_info = $model_member->getMemberInfo($contion,'member_mobile');
		output_data($member_info);

	}
	/*收银
	*/
	function add_cashOp(){
		$model_line =Model('line_cash'); 
		$member_id = $this->member_info['member_id'];
		if(!empty($_POST)){
			$model_chain =Model('chain');
			$chain=$model_chain->getChainInfo(array('chain_user'=>$_POST['chain_name'],'chain_pwd'=>md5($_POST['chain_password'])));
			$model_store=Model('store');
			$store =$model_store->getStoreInfoByID($chain['store_id']);
			//var_dump($member_id!=$store['member_id']);
			if($chain['store_id']!=$store['store_id'] ||$member_id!=$store['member_id']){
				output_error('信息错误');
			}
			$_POST['add_time']=time();
			$_POST['store_id'] = $chain['store_id'];
			$_POST['member_id']=$member_id;
			$_POST['member_name']=$this->member_info['member_name'];
			
			unset($_POST['key']);
			$result = $model_line->addLine_cash($_POST);
		
		}
		if($result){
			output_data('1');
		}else{
			output_error('失败');
		}
	}
	/*线下收银列表*/
	function line_cash_listOp(){
		$member_id = $this->member_info['member_id'];
		$model_line =Model('line_cash'); 
		$model_line->getLine_cash($chain_id);
	}
	
	public function shoukuanOp(){
        $chain_name = $_POST['chain_name'];
		$chain_pwd = $_POST['chain_pwd'];
        $member_name = $_POST['member_name'];
        $member_je = $_POST['member_amount'];
        $pamount = $member_je;
        $member_sm = $_POST['member_shuming'];
        $member_points = $member_je * 100;
        $xf_date = strtotime(date("Y-m-d H:i:s", time()));
        $model_member = Model('member');
        $member_list = $model_member->getMemberInfo(array('member_name' =>$member_name));
        $member_id = $member_list['member_id'];
        // 查询门店库存积分
        $model_chain = Model('chain');
		$chain_list = $model_chain->getChainInfo(array('chain_name' => $chain_name,'chain_pwd'=>md5($chain_pwd)));
        $store_id = $chain_list['store_id'];//门店ID

		//查询门店所属的会员账号
		$md_store = Model('store');
		$md_store_list = $md_store->getStoreInfo(array('store_id' => $store_id));
		$me_name= $md_store_list['member_name'];//自身会员号
		//判断是否是自己购买
		if($me_name==$member_name){
			//output_error('不能购买自己店铺的产品');
		}
		$m_id = $md_store_list['member_id'];
        $sj_member_info = $model_member->getMemberInfo(array('member_id' =>$m_id));
		
        //商家
        $sj_invite_one = $sj_member_info['invite_one'];
        $sj_invite_two = $sj_member_info['invite_two'];
        $sj_invite_three = $sj_member_info['invite_three'];
        $sj_member_name = $sj_member_info['member_name'];       
        
        $kcpoints = $sj_member_info['kucunpoints'];
        $m_name = $sj_member_info['member_name'];
		
       if ($member_id) {
            $pl_addtime = strtotime(date("Y-m-d"));
            // 添加消费豆
		
			$sql_xf = "insert into shopwwi_xiaofei (member_id,store_id,member_zh,member_je,member_sm,member_date) values('".$member_id."','".$chin_id."','".$member_name."',".$member_points."','".$member_sm."','".$xf_date."')";
            mysql_query($sql_xf);
			
            // 修改会员积分
            mysql_query('update shopwwi_member set member_points = member_points + ' . $member_points . ' where member_id=' . $member_id);
            // 添加积分日志
           $sql_bc = "insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$member_id},'{$member_name}',{$member_points},{$pl_addtime},'线下门店购物消费-会员宝豆增加','storepoints')";
            //Model()->execute($sql_bc);
			mysql_query($sql_bc);
            //修改库存积分
            $m_points = "-" . $member_points;
            mysql_query('update shopwwi_member set kucun_points = kucun_points - ' . $member_points . ' where member_id=' . $m_id);
           $sql_bc = "insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$m_id},'{$m_name}',{$m_points},{$pl_addtime},'线下门店购物消费-商家库存积分减少','kucunpoints')";
		
			mysql_query($sql_bc);
            //商家的会员账号返16%
            $shiliu = $member_points * 16 / 100;
            mysql_query('update shopwwi_member set member_points = member_points + ' . $shiliu . ' where member_id=' . $m_id);
            $sql_bc = "insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$m_id},'{$m_name}',{$shiliu},{$pl_addtime},'线下门店购物消费-商家宝豆返还','kucunpoints')";
			mysql_query($sql_bc);
  
            // 经验值
            $invite_one = $member_list['invite_one'];
            $invite_two = $member_list['invite_two'];
            $invite_three = $member_list['invite_three'];
            $member_name = $member_list['member_name'];
			$vip = $member_list['member_exppoints'];
            // 当前会员经验值
            if ($invite_one > 0) {
                $member_oneinfo = $model_member->getMemberInfo(array('member_id' => $invite_one));
                $member_onegradeinfo = $model_member->getOneMemberGrade(intval($member_oneinfo['member_exppoints']));
                // 上一级会员经验值
                $fy_one = $pamount * $member_onegradeinfo['nxmoney'];
                // 查询上级会员
                $m_name = $member_oneinfo['member_name'];
                $m_id = $member_oneinfo['member_id'];
                $fy_jifen = $member_points * $member_onegradeinfo['nxmoney'];
				  // 分红豆
                mysql_query('update shopwwi_member set member_red_points = member_red_points + ' . $fy_one . ' where member_id=' . $m_id);
                // 分红豆日志
               $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $member_name . "的线下订单一级分红豆返佣','redpoints','". $member_id ."' )";
				mysql_query($sql_bc);
            }
            // *******************************************
            if ($invite_two > 0) {
                $member_oneinfo = $model_member->getMemberInfo(array('member_id' => $invite_two));
                $member_onegradeinfo = $model_member->getOneMemberGrade(intval($member_oneinfo['member_exppoints']));
                // 上一级会员经验值
                $fy_one = $pamount * $member_onegradeinfo['nnxmoney'];
                // 查询上级会员
                $m_name = $member_oneinfo['member_name'];
                $m_id = $member_oneinfo['member_id'];
                $fy_jifen = $member_points * $member_onegradeinfo['nnxmoney'];
                //分红豆 
				 mysql_query('update shopwwi_member set member_red_points = member_red_points + ' . $fy_one . ' where member_id=' . $m_id);
                // 分红豆日志
               $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $member_name . "的线下订单二级分红豆返佣','redpoints','". $member_id ."' )";
				mysql_query($sql_bc);
            }   
			//以下是商家的上级
            if ($sj_invite_one > 0) {
                $member_oneinfo = $model_member->getMemberInfo(array('member_id' => $sj_invite_one));
                $member_onegradeinfo = $model_member->getOneMemberGrade(intval($member_oneinfo['member_exppoints']));
				$fy_one = $pamount * $member_onegradeinfo['sale_tc'];
                //查询上级会员
                $m_name = $member_oneinfo['member_name'];
                $m_id = $member_oneinfo['member_id'];
                $fy_jifen = $baijifen * $member_onegradeinfo['sale_tc'];
                $fy_cash = $pamount * $member_onegradeinfo['sale_tc'];
				//分红豆
			    mysql_query('update shopwwi_member set member_red_points = member_red_points + ' . $fy_one . ' where member_id=' . $m_id);
                //分红豆日志
                $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $member_name . "的订单推荐商家一级分红豆返佣','redpoints','". $member_id ."')";
				mysql_query($sql_bc);
            }
            if ($sj_invite_two > 0) {
                $member_oneinfo = $model_member->getMemberInfo(array('member_id' => $sj_invite_two));
                $member_onegradeinfo = $model_member->getOneMemberGrade(intval($member_oneinfo['member_exppoints']));
                $fy_one = $pamount * $member_onegradeinfo['nsale_tc'];
                //查询上级会员
                $m_name = $member_oneinfo['member_name'];
                $m_id = $member_oneinfo['member_id'];
                $fy_jifen = $baijifen * $member_onegradeinfo['nsale_tc'];
                $fy_cash = $pamount * $member_onegradeinfo['nsale_tc'];
                //分红豆
				mysql_query('update shopwwi_member set member_red_points = member_red_points + ' . $fy_one . ' where member_id=' . $m_id);
                //分红豆日志
                $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $member_name . "的订单推荐商家二级分红豆返佣','redpoints','". $member_id ."')";
				mysql_query($sql_bc);
            }
			output_data('收银成功');
		} else {
			output_error('账号不存在');
        }
    }
	
}
